#include "classe.h"

USING_URGE;

Player::Player(void){
    life = 100;
    label(LABEL_PLAYER);
    light.spot();
    light.color(0,100,0);
    light.intensity(100);

    linearSpeed(3.0);
    lifebar.load("media/sprites/lifebar.bmp");
    lifebar.scale(1.0,0.5);
    lifebar.position(10 + lifebar.width()/2,10 + lifebar.height()/2);

    lifebar_empty.load("media/sprites/lifebar_empty.bmp");
    lifebar_empty.scale(1.0,0.5);
    lifebar_empty.position(10 + lifebar_empty.width()/2,10 + lifebar_empty.height()/2);

}

int Player::collide(Object &other){
    if (other.label() == LABEL_ENEMY){
        if (life > 0)
        {
            life--;
            lifebar.scale(life/100.0,0.5);
            lifebar.position(10 + lifebar.width()/2,10 + lifebar.height()/2);
        }
    }

    if (other.label() == LABEL_OCEAN && other.label()!= LABEL_FLOOR){
        life-=10;
        lifebar.scale(life/100.0,0.5);
        lifebar.position(10 + lifebar.width()/2,10 + lifebar.height()/2);

        if (life <= 0)
        {
            control().disableAll();
            disableMouse();
        }
    }

    if(other.label() == LABEL_FIRE){
        if(life>0){
            life-=25;
            lifebar.scale(life/100.0,0.5);
            lifebar.position(10 + lifebar.width()/2,10 + lifebar.height()/2);
        }
    }

    return 1;
}



Enemy::Enemy(void){
    life = 100;
    label(LABEL_ENEMY);
    /*load("media/models/Cyberdemon/Cyber.md2");
    load("media/models/Cyberdemon/Cybershot.md2");
    load("media/models/Cyberdemon/CyberDie.md2");*/
    load("media/models/Mancubus/Fatso.md2");
    load("media/models/Mancubus/FatsoDeath.md2");
    load("media/models/Mancubus/FatsoFires.md2");
    load("media/models/Mancubus/FatsoMF.md2");
    load("media/models/Mancubus/FatsoPipes.md2");
    load("media/models/Mancubus/FatsoRes.md2");
    load("media/models/Mancubus/FatsoRibs.md2");
    //changeAnimation(Animation::WALK);
    //graphic().setAnimationDelay(Animation::MOVE08,10);
    //graphic().setAnimationDelay(Animation::ATTACK,8);
    //graphic().setAnimationDelay(Animation::HIT08,6);
    //changeBoundingVolume(Solid::OBOX);
    //animatedVolume();
    scale(3);
    position(38,20,40);
}

int Enemy::collide(Object &other){
    if (other.label() == LABEL_SHOT)
    {
        if (life > 0){
            life -= 10;
            tempAnimation(Animation::ATTACK, Animation::WALK);
        }
        else{
            load("media/models/Mancubus/FatsoDeath.md2");
            visible(false);
            inactiveBody();
        }
    }

    if(other.label() == LABEL_FIRE){
        life-=25;
        fire.visible(true);
        if (life > 0){
            life -= 10;
            tempAnimation(Animation::ATTACK, Animation::WALK);
        }
        else{
            load("media/models/Mancubus/FatsoDeath.md2");
            visible(false);
            inactiveBody();
        }
    }

    return 1;
}

void Portal::act(void)
{
    light.position() = position();
    timer--;
    if (timer <= 0)    disappear();
}

void Portal::appear(void){
    timer = 2000;
    visible(true);
}

void Portal::disappear(void){
    timer = 0;
    visible(false);
}

Portal::Portal(void){

}

PortalIn::PortalIn(){
     //scale(5.0);
    //load("media/tex/portal.png");
    sphere();
    scale(5,7,0);
    loadTexture("media/tex/portalAzul.png");
    body().damping(1);
    disappear();
}

int PortalIn::collide(Object &other){
    if (other.label() == LABEL_ENEMY|| other.label() == LABEL_PLAYER || other.label() == LABEL_OBJECT){
        Math::Array shift(-10, -10, -10);
        if(positionOut != shift){
            other.body().pos = positionOut;
            disappear();
        }
    }
return 1;
}

void PortalIn::setPositionOut(Math::Array positionout){
    //this.positionOut = positionOut;
    positionOut = positionout;
}

/*Math::Array PortalIn::getPositionOut(void){
    return positionOut;
}*/

PortalOut::PortalOut(){
    //scale(5.0);
    //load("media/tex/portal.png");
    //sphere();
    sphere();
    scale(5,7,0);
    loadTexture("media/tex/portalLaranja.png");
    s_block.load("media/sounds/LockNLoad3.wav");
    body().damping(1);
    disappear();
}

int PortalOut::collide(Object &other){
    if (other.label() == LABEL_ENEMY|| other.label() == LABEL_PLAYER || other.label() == LABEL_OBJECT){
        s_block.play();
        disappear();
    }
return 1;
}


Shot::Shot(void){
    sphere();
    changeBoundingVolume(Solid::SPHERE);
    label(LABEL_SHOT);
    scale(2.0);
    body().gravityScale(0.0);
    body().damping(0);

    light.intensity(5.0);
    light.attenuation(0.4);
    light.quadraticAttenuation(0.01);

    visible(false);
    disappear();

    p.generate(Particle::GLOW);
    p.setAnimationType(Particle::FIRE);
    p.scale(0.75);

    spark.load("media/sprites/spark.tga");
    spark.setAnimationType(Particle::FOG);
    spark.scale(1);
    spark.animationMotionOffset(1.5);
    spark.stop();
}

void Shot::act(void)
{
    p.position() = position();
    light.position() = position();

    if (p.isEmitting())
    {
        timer--;
        light.intensity(float(timer)*0.0715);
        if (timer <= 0)    disappear();
    }
    if(mouseButton == LEFT_BUTTON){
        //light.color(255,180,60);
        light.color(0,191,255);
        p.color(0,0,255);
        spark.color(0,0,205);
        if(portalblue.visible())  portalblue.disappear();
    }
    if(mouseButton == RIGHT_BUTTON){
        light.color(255,215,0);
        p.color(255,165,0);
        spark.color(255,140,0);
        if(portalorange.visible())  portalorange.disappear();
    }
    if(!portalorange.visible()){
        Math::Array shift(-10, -10, -10);
        portalblue.setPositionOut(shift);
    }
}

int Shot::collide(Object &other){

    if(mouseButton == LEFT_BUTTON){
        if(other.label() == LABEL_WALL||other.label() == LABEL_FLOOR){
            if(other.label() == LABEL_WALL){
                mouseButton = NONE_BUTTON;
                portalblue.appear();
                portalblue.position() = position();
                portalblue.direction(-1,0,0);
                disappear();
            }
            else{
                mouseButton = NONE_BUTTON;
                portalblue.appear();
                portalblue.position() = position();
                portalblue.direction(0,-1,0);
                disappear();
            }
        }
        if(other.label() == LABEL_ENEMY || other.label() == LABEL_OBJECT){
            if(portalorange.visible()){
                mouseButton = NONE_BUTTON;
                other.body().pos = portalblue.positionOut;
                portalblue.disappear();
                portalorange.disappear();
                disappear();
            }
            else{
                mouseButton = NONE_BUTTON;
                portalblue.appear();
                portalblue.position() = position();
                portalblue.direction(-1,0,0);
                disappear();
            }
        }
    }

    if(mouseButton == RIGHT_BUTTON){
        if(other.label() == LABEL_WALL||other.label() == LABEL_FLOOR){
            if(other.label() == LABEL_WALL||other.label() == LABEL_ENEMY || other.label() == LABEL_OBJECT){
                Math::Array shift(1, 1, 0);
                mouseButton = NONE_BUTTON;
                portalorange.appear();
                portalorange.position() = position();
                portalblue.direction(-1,0,0);
                portalblue.setPositionOut(position() + shift);
                disappear();

            }
            else{
                Math::Array shift(1, 1, 0);
                mouseButton = NONE_BUTTON;
                portalorange.appear();
                portalorange.position() = position();
                portalblue.direction(0,0,-1);
                portalblue.setPositionOut(position() + shift);
                disappear();
            }
        }
    }

    return 1;
}


void Shot::appear(void){
    timer = 70;
    light.intensity(float(timer)*0.0715);
    p.start();
    activeBody();
}

void Shot::disappear(void){
    light.intensity(0.0);
    timer = 0;
    visible(false);
    p.stop();
}


Wall::Wall(void){
    box();
    label(LABEL_WALL);
    loadTexture("media/tex/bricks.bmp");
    staticBody();
}


Flag::Flag(void){
    label(LABEL_FLAG);
    load("media/models/flag/flag.md2");
    scale(5);
    position(-40,12,-40);
    staticBody();
}

Casket::Casket(void){
    label(LABEL_OBJECT);
    load("media/models/Casket/casket.md2");
    scale(3);

}
int Casket::collide(Object &other){
    if (other.label() == LABEL_PLAYER || other.label() == LABEL_ENEMY){
        fire.position() = position();
        visible(false);
        fire.visible(true);
    }
    return 1;
}

Fire::Fire(void){
    label(LABEL_FIRE);
    load("media/models/fire/fire.md2");
    scale(5);
    visibleVolume(false);
    transparency(0.25);
    staticBody();
    visible(false);
}

